import { useEffect } from "react";
import { useForm } from "@sker/sdk";
import { BetaSchemaForm } from "@ant-design/pro-form";

export const UserAdd: React.FC<{ value?: any, onFinish?: (values: any) => Promise<void> }> = ({ value, onFinish }) => {
    useEffect(() => {
        form.setFieldsValue(value)
    }, [value])
    const { props: formProps, form } = useForm([
        {
            title: '用户名',
            dataIndex: 'username',
            proFieldProps: {
                readonly: value?.uid ? true : false
            },
            fieldProps: {
                autoComplete: 'off'
            },
            formItemProps: {
                rules: [{ required: true, message: '请输入用户名' }],
            }
        },
        {
            title: '密码', dataIndex: 'password', valueType: 'password',
            fieldProps: {
                autoComplete: 'off'
            },
            formItemProps: {
                rules: [{ required: true, message: '请输入用户名' }],
            }
        },
        {
            title: '姓名',
            dataIndex: 'realname',
            fieldProps: {
                autoComplete: 'off'
            }
        },
        {
            title: '手机号',
            dataIndex: 'mobile',
            fieldProps: {
                autoComplete: 'off'
            }
        },
    ], async (values) => {
        if (value) {
            values.uid = value.uid;
        }
        onFinish && await onFinish(values)
    })
    return <BetaSchemaForm {...formProps} />
}